package cn.jinjimi.app.presenter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;

import cn.jinjimi.app.AccountManager;
import cn.jinjimi.app.controller.UserController;
import cn.jinjimi.app.controller.event.LoginSuccessEvent;
import cn.jinjimi.app.controller.event.SelectPagerEvent;
import cn.jinjimi.app.entity.AppConfig.HomeEntranceList;
import cn.jinjimi.app.interfaces.IView;
import cn.jinjimi.app.interfaces.IMainView;
import cn.jinjimi.app.managers.LocalPushActInfoManager;
import cn.jinjimi.app.managers.NoticeManager;
import cn.jinjimi.app.store.entity.NoticeEntity;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;

/**
 * main activity 的presenter
 * <p/>
 * 初始化IM
 * Created by jinsen on 15/9/16.
 */
public class MainPresenter extends NetBasePresenter {

    Context mContext;
    IMainView mView;

    HashMap<String, Bitmap> mMainTabsDrawbaleCache;

    public HomeEntranceList mTabs;

    public MainPresenter(IView view, Context context) {
        super(view);

        mView = (IMainView) view;
        this.mContext = context;

        mMainTabsDrawbaleCache = new HashMap<>();

        NoticeManager.getNoticesByCode(NoticeEntity.TypeBigDay, NoticeEntity.TypeNewFeature,
                NoticeEntity.TypeNewVersion, NoticeEntity.TypeAlert);

        startService();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onSelectPagerEvent(SelectPagerEvent event) {
        mView.selectPager(event.position);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onLoginSuccessEvent(LoginSuccessEvent event) {
        // 刷新个人信息数据
        UpdateUserInfo();

        //用户登录状态发生变化，重新设置红点
        NoticeManager.getNoticesByCode();

        LocalPushActInfoManager.clearActivityInfoCache(mContext);
    }

    private void UpdateUserInfo() {
        if (AccountManager.getInstance().isLogin()) {
            UserController.updateUserInfo();
        }
    }

    /**
     * 启动播放服务
     */
    private void startService() {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mView = null;
        mTabs = null;
    }

}